package br.com.foxinline.servico;

import br.com.foxinline.generico.ServicoGenerico;
import br.com.foxinline.modelo.Coordenada;
import br.com.foxinline.modelo.Quadra;
import br.com.foxinline.modelo.Setor;
import br.com.foxinline.util.Msg;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
public class CoordenadaServico extends ServicoGenerico<Coordenada> {

    public CoordenadaServico() {
        super(Coordenada.class);
    }

    @Override
    public void delete(Coordenada entity) {
        try {
            super.delete(entity);
            Msg.messagemInfo(Msg.SuccessFull);

        } catch (Exception e) {

            System.err.println(e);
            Msg.messagemError(Msg.Failure);
        }
    }

    public List<Coordenada> find(Setor setor) {

        if (setor != null && setor.getId() != null) {
            String sql = "select c from " + setor.getClass().getSimpleName() + " s JOIN s.coordenadas c WHERE s.id = :id order by c.id";
            Query query = getEntityManager().createQuery(sql);
            query.setParameter("id", setor.getId());

            return query.getResultList();
        }

        return new ArrayList<Coordenada>();
    }

    public List<Coordenada> find(Quadra quadra) {

        if (quadra != null && quadra.getId() != null) {
            String sql = "select c from " + quadra.getClass().getSimpleName() + " b JOIN b.coordenadas c WHERE b.id = :id order by c.id";
            Query query = getEntityManager().createQuery(sql);
            query.setParameter("id", quadra.getId());

            return query.getResultList();
        }

        return new ArrayList<Coordenada>();
    }
}
